﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class MembershipCard_wechat_web_shouhuo : WeiPage
{
    PersonInfoBO fbo = new PersonInfoBO();
    ShoppingCartBO sbo = new ShoppingCartBO();
    REDP_HEADBO rhbo = new REDP_HEADBO();
    REDP_DETAILBO rdbo = new REDP_DETAILBO();
    PRODUCTBO pbo = new PRODUCTBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=d9bfd969e149c1844698cfd1f962bdad&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = "" + GetAppid() + "";
                    }
                }
                catch (Exception)
                {

                }
            }
            UploadSH();
        }
    }

    private void UploadSH()
    {
        if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
        {
            string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
            string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
            Session["FromUserName"] = user;
            Session["ToUserName"] = user2;
            PersonInfo per = fbo.GetModel(user);
            if (per != null)
            {
                List<ShoppingCart> list = sbo.GetModelList(" pid=" + per.ID);
                if (list.Count > 0)
                {
                    sumcar.InnerHtml = "<span><a href='ShopCar.aspx'><em class='indexicon4'></em><i>" + list.Count + "</i></a></span>";
                }
                username.InnerHtml = per.Name;
                userjf.InnerHtml = per.SurplusIntegral + "分";

                orderlist.InnerHtml = "";
                List<REDP_HEAD> lrh = rhbo.GetModelList(" MEMBERNO='" + per.PersonNo + "' and  STATUS<>'已收货' ");
                foreach (REDP_HEAD r in lrh)
                {
                    int sl = 0; //数量
                    orderlist.InnerHtml += " <div class='order_pin'><strong>兑换产品</strong>";
                    List<REDP_DETAIL> lrd = rdbo.GetModelList(" pid=" + r.ID);
                    foreach (REDP_DETAIL d in lrd)
                    {
                        PRODUCT p = pbo.GetModel(d.SKU);
                        if (p != null)
                        {
                            orderlist.InnerHtml += " <p><a href='#'><img src='" + (p.PicUrl == "" ? "images/m.png" : p.PicUrl) + "' /></a><span>" + p.Name + "</span>";
                            sl += int.Parse(d.UNITS.ToString());
                        }
                    }
                    orderlist.InnerHtml += " <div class='zongji'><span>共计：<bdo>" + r.POINTS + "分</bdo></span> <bdo>" + r.STATUS + "</bdo>&nbsp;  共计" + sl + "件商品</div></div> <div class='carbtn'> <ol><div class='duihuanbtn'  onclick='return sh("+r.ID+")'>确认收货</div></ol> <ol><div class='goubtn'>查看物流</div></ol> </div>";
                }
                if (lrh.Count == 0)
                {
                    orderlist.InnerHtml = "<div style='text-align:center; color:Gray;'><br /><br /><br /><br /><br /><br />无兑换记录</div>";
                }
            }
            else
            {
                // addcar.InnerHtml = "<font color='red'>请先注册和绑定</font>";
                Response.Redirect("index2.aspx");
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            REDP_HEAD r = rhbo.GetModel(int.Parse(rid.Value));
            if (r != null)
            {
                r.STATUS = "已收货";
                rhbo.Update(r);
                UploadSH();
                
            }
        }
        catch (Exception)
        {
            
        }
    }
}